package com.whfc.common.file.impl;

import com.whfc.common.file.FileHandler;
import com.whfc.common.file.properties.AliyunFileProperties;
import com.whfc.common.file.properties.FileProperties;
import io.minio.MinioClient;
import io.minio.ObjectStat;
import io.minio.PutObjectOptions;
import io.minio.errors.InvalidEndpointException;
import io.minio.errors.InvalidPortException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description miniIO文件处理
 * @Author hw
 * @Date 2021-05-12 18:05
 * @Version 1.0
 */
public class MinioFileHandler implements FileHandler {

    private final static Logger logger = LoggerFactory.getLogger(AliyunOssFileHandler.class);

    /**
     * 应用ID
     */
    private String accessKeyId;

    /**
     * 秘钥
     */
    private String accessKeySecret;

    /**
     * endpoint
     */
    private String endpoint;

    /**
     * bucket
     */
    private String bucket;

    /**
     * 访问域名
     */
    private String host;

    public MinioFileHandler(FileProperties properties) {
        AliyunFileProperties fileProperties = (AliyunFileProperties) properties.getChild();
        accessKeyId = fileProperties.getKey_id();
        accessKeySecret = fileProperties.getKey_secret();
        endpoint = fileProperties.getEndpoint();
        bucket = fileProperties.getBucket();
        host = fileProperties.getSave_url();
    }

    private MinioClient client() throws InvalidPortException, InvalidEndpointException {
        MinioClient client = new MinioClient(endpoint, accessKeyId, accessKeySecret);
        return client;
    }

    @Override
    public String upload(String path, InputStream is) {
        String url = null;
        try {
            PutObjectOptions objectOptions = new PutObjectOptions(is.available(), 5 * 1024 * 1024);
            client().putObject(bucket, path, is, objectOptions);
            url = parseUrl(path);
        } catch (Exception e) {
            logger.error("文件上传失败", e);
        }
        return url;
    }

    @Override
    public String upload(String path, InputStream is, String ex) {
        return upload(path, is);
    }

    @Override
    public String upload(String path, InputStream is, Map<String, String> headers) {
        return upload(path, is);
    }

    @Override
    public String upload(String path, InputStream is, Map<String, String> headers, Map<String, String> tags) {
        return upload(path, is);
    }

    @Override
    public void download(String key, String saveFilePath) {
        try {
            client().getObject(bucket, key, saveFilePath);
        } catch (Exception e) {
            logger.error("文件下载失败", e);
        }
    }

    @Override
    public Long getFileSize(String ossKey) {
        return null;
    }

    @Override
    public void delete(List<String> fileUrls) {
        try {
            List<String> fileUrl = fileUrls.stream().filter(url -> !"".equals(url))
                    .map(this::getPath)
                    .distinct()
                    .collect(Collectors.toList());
            Iterable<String> ss = () -> fileUrl.listIterator(fileUrl.size());
            client().removeObjects(bucket, ss);
        } catch (Exception e) {
            logger.error("文件删除失败", e);
        }
    }

    @Override
    public void delete(String url) {
        try {
            client().removeObject(bucket, url);
        } catch (Exception e) {
            logger.error("文件删除miniIO失败", e);
        }
    }

    @Override
    public boolean checkFile(String path) {
        try {
            ObjectStat objectStat = client().statObject(bucket, path);
            if (objectStat != null) {
                return true;
            }
        } catch (Exception e) {
            logger.error("查询文件miniIO失败", e);
        }
        return false;
    }

    @Override
    public Map<String, String> getPolicy(String dir, String callbackUrl) {
        return null;
    }

    @Override
    public Map<String, String> getPolicy(String dir) {
        return null;
    }

    @Override
    public String getAuthFileUrl(String path) {
        try {
            String url = client().presignedGetObject(bucket, path);
            return url;
        } catch (Exception e) {
            logger.error("查询url miniIO失败", e);
        }
        return null;
    }

    @Override
    public String getPath(String url) {
        return url.replace(host + "/", "");
    }

    @Override
    public String parseUrl(String path) {
        return host + "/" + path;
    }

    @Override
    public String parsOssUrl(String key) {
        return null;
    }

    @Override
    public void rename(String key, String newName) {

    }
}
